$(document).ready(function(){
try {
initData();
}catch (e) {
  console.log('dataTable init error');
}
#if($!saveMethod == '1')
validateCompanyForm();

$(".add-submit-btn").bind("click", function(){
if (! $(".$!{pageName}_form").valid()) {
return;
}
var params = $(".$!{pageName}_form").serialize();
//var serializeJson = $("#signupForm").serializeArray();

$.ajax({
type : "POST",
url : basePath + "/nht/doc/${lowerModelName}/save${modelName}",
data : params,
success : function(result){
if(result.code == 200){
sucessTips("新增成功");
previousPage();
return;
}
sucessTips("新增失败");
}
});
});
#end

$(".$!{pageName}_remove").bind("click", function(){
var checkedRows = filterChecked();
if(checkedRows.length == 0){
warningTips("请选择要删除的数据");
return ;
}

layer.confirm('确定要删除？', {
btn: ['确定','取消']
}, function(){

var array = "";
$.each(checkedRows, function(i, item){
var id = $(item).val();
array = array + id + ","
});
array = array.substring(0, array.length-1);

$.ajax({
type:"POST",
url: basePath + '/nht/doc/${lowerModelName}/remove',
data: {"id" : array},
success: function(data){
if(data.code != 200){
errorTips("删除失败");
return;
}
sucessTips("删除成功");
$('.dataTables_$!{pageName}').dataTable().fnDraw(false);
}
});

});

});

#if($!saveMethod == '1')
$(".$!{pageName}_add_page").bind("click", function(){
location.href = basePath + "/nht/doc/${lowerModelName}/showAddPage";
});
#end

});
#if($!saveMethod == '1')
/**
* 表单校验
* @returns
*/
function validateCompanyForm(){

    var icon = "<i class='fa fa-times-circle'></i> ";
    $(".$!{pageName}_form").validate({
        focusInvalid:true,
            rules: {
                deviceName: {
                required: true
            },
            deviceCode: {
                required: true
            }
            },
            messages: {
                deviceName: {
                required: icon + "请输入名称"
            },
            deviceCode: {
                required: icon + "请输入编码"
            }
        }
    });

}
#end

function initData(){

var config = {
"ajax": {
url : basePath + '/nht/doc/${lowerModelName}/loadData',
"type": "POST"
},
"columns":[
{
"data":"id",
//"visible":false
"render": function ( data, type, full, meta ) {
var html = '<input type="checkbox" class="checkbox i-checks check-item" value="' + data + '">';
return html;
}
},
{"data":"varietyName"},
{"data":"farmlandName"},
{"data":"userName"},
{"data":"plantTime",
"render": function (data, type, row) {
var date = new Date(parseInt(data));
return date.format("yyyy-MM-dd hh:mm:ss")
}
},
{
"data":"operate",
"render": function (data, type, row) {
var html = '<button class="btn btn-primary $!{pageName}_detail" type="button" data-id="' + row.id + '">详情</button>';
#if($!saveMethod == '1')
html = html + '<button class="btn btn-danger $!{pageName}_update" type="button" data-id="' + row.id + '">编辑</button>';
    #end
return html;
}
}

],
"fnServerParams":function(params){
params.offset = params.start;
params.pageSize = params.length;
},
"fnDrawCallback": function(options){
initCropEven();
initCheckItem();
}
};
if ($('.dataTables_$!{pageName}').length > 0) {
$('.dataTables_$!{pageName}').initDataTable(config);
}
}

/**
* 绑定事件
*/
function initCropEven(){
var details = $(".$!{pageName}_detail");
$.each(details, function(i, item){
$(item).bind("click", function(){
var id = $(this).attr("data-id");
showDetail("$!{annotation}详情", id);
});
});
#if($!saveMethod == '1')
    var updates = $(".$!{pageName}_update");
    $.each(updates, function (i, item) {
    $(item).bind("click", function () {
    var id = $(this).attr("data-id");
    location.href = basePath + "/nht/doc/${lowerModelName}/showAddPage?id="+id;
    });
    });
#end
}


/**
*  显示弹出层
* @param type
* @param title
* @returns
*/
function showDetail(title, id){
$.ajax({
type:"GET",
url: basePath + '/nht/doc/${lowerModelName}/showDetail',
data: {"id": id},
success: function(data){
console.log(data);
layer.open({
title: title,
type: 1,
skin: 'layui-layer-rim',
area: ['620px', '450px'],
content: data
});
}
});
}